package com.wlstock.funi.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.wlstock.funi.R;
import com.wlstock.funi.app.FundApplication;
import com.wlstock.funi.model.OperateModel;
import com.wlstock.funi.model.SelfFundModel;
import com.wlstock.funi.utils.ToastUtils;
import com.wlstock.funi.widget.TradeSelector;

import java.util.List;

public class SelfFundAdapter extends BaseAdapter implements OnClickListener {
	private Context mContext;
	private List<SelfFundModel> models;
	private WebViewClient mWebClient = new WebViewClient(){
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			return true;
		}

	};

	public SelfFundAdapter(Context mContext){
		this.mContext = mContext;
	}
	
	public SelfFundAdapter(Context mContext,List<SelfFundModel> models){
		this.mContext = mContext;
		this.models   = models;  
	}
	
	public List<SelfFundModel> getModels() {
		return models;
	}

	public void setModels(List<SelfFundModel> models) {
		this.models = models;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return models.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return models.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int position, View view, ViewGroup viewGroup) {
		// TODO Auto-generated method stub
		Holder holder = null;
		if (view == null) {
			view = LayoutInflater.from(mContext).inflate(R.layout.list_item_self_fund, viewGroup, false);
			holder = new Holder();
			holder.tv_sf_stk_info = (TextView) view.findViewById(R.id.tv_sf_stk_info);
			holder.tv_sf_stk_price_info = (TextView) view.findViewById(R.id.tv_sf_stk_price_info);
			holder.img_sf_stk_dayK = (ImageView) view.findViewById(R.id.img_sf_stk_dayK);
			holder.btn_op_buy = (Button) view.findViewById(R.id.btn_op_buy);
			holder.btn_op_sell= (Button) view.findViewById(R.id.btn_op_sell);
			holder.tv_sf_stk_backinfo = (TextView) view.findViewById(R.id.tv_sf_stk_backinfo);

			view.setTag(holder);
		}else {
			holder = (Holder) view.getTag();
		}
		SelfFundModel selfFundModel = models.get(position);
		holder.tv_sf_stk_info.setText(selfFundModel.nameWithCode());
		holder.tv_sf_stk_price_info.setText(getPriceInfoText(selfFundModel));
		if (selfFundModel.getBackupInfo()!=null){
			holder.tv_sf_stk_backinfo.setVisibility(View.VISIBLE);
			holder.tv_sf_stk_backinfo.setText(selfFundModel.getBackupInfo());
		}

		ImageLoader.getInstance().displayImage(selfFundModel.getStkDayK(), holder.img_sf_stk_dayK);

		holder.btn_op_buy.setTag(selfFundModel);
		holder.btn_op_sell.setTag(selfFundModel);
		holder.btn_op_buy.setOnClickListener(this);
		holder.btn_op_sell.setOnClickListener(this);

		return view;
	}

	private CharSequence getPriceInfoText(SelfFundModel selfFundModel) {
		// TODO 修饰价格信息
		if (selfFundModel.getlastClose()==0) {
			return "";
		}
		StringBuffer sb = new StringBuffer();
		sb.append("今开:").append(selfFundModel.getCurOpen()+"\n").
			append("昨收:").append(selfFundModel.getlastClose()+"\n").
			append("最高:").append(selfFundModel.gethPrice()+"\n").
			append("最低:").append(selfFundModel.getLowPrice()+"\n").
			append("现价:").append(selfFundModel.getCurPrice()+" ").append(selfFundModel.cur2CloseDisAbsDisStr());
		return sb.toString();
	}


	public class Holder{
		TextView tv_sf_stk_info;
		TextView tv_sf_stk_price_info;
		ImageView img_sf_stk_dayK;
		Button   btn_op_buy;
		Button   btn_op_sell;
		TextView tv_sf_stk_backinfo;
	}


	@Override
	public void onClick(View v) {
		if (!FundApplication.isLogSuccess) {
			ToastUtils.showMiddleMsg(mContext, "请先登录");
			return;
		}
		
		SelfFundModel model = (SelfFundModel) v.getTag();
		switch (v.getId()) {
		case R.id.btn_op_buy:
			TradeSelector selB = new TradeSelector(mContext, model.getStockName(), model.getStockNo(),String.valueOf(model.getCurPrice()));
			selB.show(OperateModel.TradeType_BUY);
			break;
		case R.id.btn_op_sell:
			TradeSelector selS = new TradeSelector(mContext, model.getStockName(), model.getStockNo(),String.valueOf(model.getCurPrice()));
			selS.show(OperateModel.TradeType_SELL);
			break;
		default:
			break;
		}
		
	}
	
	
	
	
}
